package com.infitecs.eyas.security.service;

import com.infitecs.eyas.security.dao.RoleDao;
import com.infitecs.eyas.security.dao.UserDao;
import com.infitecs.eyas.security.model.Role;
import com.infitecs.eyas.security.model.User;
import com.infitecs.krill.autocomplete.service.AutoCompleteService;
import com.infitecs.krill.constant.Constant;
import com.infitecs.krill.exception.ValidateErrorException;
import com.infitecs.krill.mybatis.interceptor.PageInterceptor;
import com.infitecs.krill.mybatis.model.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Service
public class UserService {

    @Autowired
    private UserDao userDao;
    @Autowired
    private RoleDao roleDao;
    @Autowired
    private AutoCompleteService autoCompleteService;

    public User getUserByUsername(String username) {
        return userDao.getUserByUsername(username);
    }

    public List<User> getUserListPage(Page page,User user) {
        PageInterceptor.startPage(page);
        return userDao.getUserListPage(user);
    }

    public boolean addUser(User user) {
        validateRepeatUser(user);
        userDao.addUser(user);
        addUserRole(user);
        autoCompleteService.initAutoComplete();
        return user.getUserId() != null;
    }

    private void addUserRole(User user) {
        if (user.getRoleList() != null && !user.getRoleList().isEmpty()) {
            userDao.addUserRole(user);
        }
    }

    public User getUser(String userId) {
        return userDao.getUser(userId);
    }


    public int updateUser(User user) {
        if (!userDao.getUser(user.getUserId()).getUsername().equals(user.getUsername())) {
            validateRepeatUser(user);
        }
        userDao.deleteUserRoleByUserId(user.getUserId());
        addUserRole(user);
        int result = userDao.updateUser(user);
        autoCompleteService.initAutoComplete();
        return result;
    }


    private void validateRepeatUser(User user) {
        if (userDao.getUserByUsername(user.getUsername()) != null) {
            throw new ValidateErrorException("usernameAlreadyExists");
        }
    }


    public boolean deleteUser(String userId) {
        userDao.deleteUserRoleByUserId(userId);
        boolean result = userDao.deleteUser(userId) == Constant.AFFECTED_LINE_1;
        autoCompleteService.initAutoComplete();
        return result;
    }


    public List<Role> getRolesByUsername(String username) {
        return roleDao.getRolesByUsername(username);
    }


    public List<String> getPermTokensByUsername(String username) {
        List<String> rolePermTokens = roleDao.getRolePermTokensByUsername(username);
        Set<String> set = new HashSet<>();
        for (String permToken : rolePermTokens) {
            set.add(permToken);
        }
        return new ArrayList<>(set);
    }


    public int updatePassword(String loginName, String oldPassword, String newPassword) {
        User user = userDao.getUserByUsername(loginName);
        if (!user.getPassword().equals(oldPassword)) {
            throw new ValidateErrorException("oldPwdWrong");
        }
        user.setPassword(newPassword);
        return userDao.updatePassword(user);
    }

    public List<User> getUserList() {
        return userDao.getUserList();
    }
}
